package com.link510.aitools.activity.airobot.picture;


import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.GridView;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.blankj.utilcode.util.ToastUtils;
import com.google.common.collect.Lists;
import com.link510.aitools.R;
import com.link510.aitools.core.data.model.QiniuFileListingModel;
import com.link510.aitools.core.domain.qiniu.QiniuFileInfo;
import com.link510.aitools.core.helper.UnixTimeHelper;
import com.link510.aitools.core.listen.CWMListener;
import com.link510.aitools.core.message.MessageInfo;
import com.link510.aitools.framework.activity.BaseAcitivity;
import com.link510.aitools.framework.adapter.PictureAdapter;
import com.link510.aitools.services.Airobots;
import com.link510.aitools.services.CustomWaitDialogUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;

public class RobotPictureActivity extends BaseAcitivity {

    @BindView(R.id.dp_dickdate)
    TextView dateTextView;

    @BindView(R.id.picture_file_gridview)
    GridView pictureGridView;

    /**
     * 图片适配器
     */
    private PictureAdapter pictureAdapter;

    /**
     * 当前时间戳
     */
    private Integer timestamp;

    /**
     * 下一页标记
     */
    private String market = "";

    /**
     * 是否触底
     */
    private boolean eof = false;

    /**
     * 图片列表
     */
    private List<QiniuFileInfo> qiniuFileInfoList = null;

    private CustomWaitDialogUtils customWaitDialogUtils = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_robot_picture);
        initUI();
        initData();
    }

    private void initUI() {

        customWaitDialogUtils = new CustomWaitDialogUtils(this.mContext);
        pictureAdapter = new PictureAdapter(this.mContext) {
            @Override
            public List<QiniuFileInfo> getPictureList() {

                if (qiniuFileInfoList == null) {
                    qiniuFileInfoList = Lists.newArrayList();
                }

                return qiniuFileInfoList;
            }

            @Override
            public void toAction(QiniuFileInfo info) {

            }
        };

        pictureGridView.setAdapter(pictureAdapter);
    }

    private void initData() {
        timestamp = UnixTimeHelper.getInstance().getUnixTimeStamp();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.robot_piture_right_topmenu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    // 菜单的监听方法
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == R.id.select_date_item) {
            selectDateTime();
        }

        return true;
    }


    /**
     * 选择时间
     */
    public void selectDateTime() {

        Calendar startDate = Calendar.getInstance();
        startDate.add(Calendar.MONTH, -3);
        Calendar endDate = Calendar.getInstance();


        TimePickerView pvTime = new TimePickerBuilder(this, new MyOnTimeSelectListener())
                .setType(new boolean[]{true, true, true, false, false, false})// 默认全部显示
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确定")//确认按钮文字
//        .setContentSize(18)//滚轮文字大小
//        .setTitleSize(20)//标题文字大小
//        .setTitleText("Title")//标题文字
//        .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
//        .isCyclic(true)//是否循环滚动
//        .setTitleColor(Color.BLACK)//标题文字颜色
//        .setSubmitColor(Color.BLUE)//确定按钮文字颜色
//        .setCancelColor(Color.BLUE)//取消按钮文字颜色
//        .setTitleBgColor(0xFF666666)//标题背景颜色 Night mode
//        .setBgColor(0xFF333333)//滚轮背景颜色 Night mode
//        .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)//起始终止年月日设定
//        .setLabel("年","月","日","时","分","秒")//默认设置为年月日时分秒
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .isDialog(true)//是否显示为对话框样式
                .build();


        pvTime.show();
    }

    /**
     * 处理时间
     *
     * @param date 日期型
     * @return date 日期型
     */
    private String getTime(Date date) {
        return UnixTimeHelper.getInstance().getShowDate(date); //date.getYear() + "-" + date.getMonth() + "-" + date.getDay();
    }


    private class MyOnTimeSelectListener implements OnTimeSelectListener {

        @Override
        public void onTimeSelect(Date date, View v) {


            RobotPictureActivity.this.dateTextView.setText(getTime(date));
            RobotPictureActivity.this.timestamp = UnixTimeHelper.getInstance().getUnixTimeStamp(date);
            RobotPictureActivity.this.market = "";
            RobotPictureActivity.this.eof = false;
            RobotPictureActivity.this.qiniuFileInfoList.clear();
            RobotPictureActivity.this.pictureAdapter.notifyDataSetChanged();
            RobotPictureActivity.this.reloadPictureList();

        }
    }

    /**
     * 加载图片
     */
    private void reloadPictureList() {

        customWaitDialogUtils.show("图片加载中");

        Airobots.getInstance().pictureList(RobotPictureActivity.this.deviceSN,
                RobotPictureActivity.this.timestamp,
                RobotPictureActivity.this.market,
                12,
                new CWMListener<MessageInfo<QiniuFileListingModel>>() {
                    @Override
                    public void onSuccess(String message, MessageInfo<QiniuFileListingModel> messageInfo) {


                        if (messageInfo == null) {
                            ToastUtils.showShort("图片加载失败");
                            customWaitDialogUtils.close();
                            return;
                        }

                        QiniuFileListingModel model = messageInfo.getContent();

                        if (model == null) {
                            ToastUtils.showShort("图片加载失败");
                            customWaitDialogUtils.close();
                            return;
                        }

                        RobotPictureActivity.this.market = model.getMarker();
                        RobotPictureActivity.this.eof = model.isEof();

                        if (model.getQiniuFileInfoList() == null || model.getQiniuFileInfoList().size() <= 0) {
                            customWaitDialogUtils.close();
                            return;
                        }

                        if (RobotPictureActivity.this.qiniuFileInfoList == null) {
                            RobotPictureActivity.this.qiniuFileInfoList = Lists.newArrayList();
                        }

                        RobotPictureActivity.this.qiniuFileInfoList.addAll(model.getQiniuFileInfoList());

                        customWaitDialogUtils.close();
                        ToastUtils.showShort("图片加载成功");
                        RobotPictureActivity.this.runOnUiThread(() -> RobotPictureActivity.this.pictureAdapter.notifyDataSetChanged());

                    }

                    @Override
                    public void onError(int code, String message) {

                        customWaitDialogUtils.close();
                        ToastUtils.showShort("图片加载失败");
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (customWaitDialogUtils != null) {
            customWaitDialogUtils.dismiss();
        }
    }
}